package com.stylefeng.guns.modular.verbal.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.config.properties.GunsProperties;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.SuccessTip;
import com.stylefeng.guns.core.common.constant.factory.ConstantFactory;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.support.BeanKit;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.ArticleMapper;
import com.stylefeng.guns.modular.system.model.Article;
import com.stylefeng.guns.modular.verbal.service.IArticleService;
import com.stylefeng.guns.modular.verbal.warpper.ArticleWarpper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 文章控制器
 *
 * @author fengshuonan
 * @Date 2018-04-10 13:07:03
 */
@Controller
@RequestMapping("/article")
public class ArticleController extends BaseController {

    private String PREFIX = "/verbal/article/";

    @Autowired
    private IArticleService articleService;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private GunsProperties gunsProperties;

    /**
     * 跳转到文章首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "article.html";
    }

    /**
     * 跳转到添加文章
     */
    @RequestMapping("/article_add")
    public String articleAdd() {
        return PREFIX + "article_add.html";
    }

    /**
     * 跳转到修改文章
     */
    @RequestMapping("/article_update/{articleId}")
    public String articleUpdate(@PathVariable Integer articleId, Model model) {
        Article article = articleService.selectById(articleId);
        Map<String, Object> map = BeanKit.beanToMap(article);
        model.addAttribute("item", new ArticleWarpper(map).warp());
        LogObjectHolder.me().set(article);
        return PREFIX + "article_edit.html";
    }

    /**
     * 获取文章列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String title, String typeCode) {
        Page<Article> page = new PageFactory<Article>().defaultPage();
        List<Map<String, Object>> list = articleMapper.list(page, title, typeCode);
        page.setRecords((List<Article>) new ArticleWarpper(list).warp());
        return super.packForBT(page);

    }

    /**
     * 新增文章
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @ApiOperation(value = "添加文章", tags = "话术", httpMethod = "POST", code = 200, response = SuccessTip.class)
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "pic", value = "图片url", dataType = "string", required = true),
                    @ApiImplicitParam(name = "title", value = "标题", dataType = "string", required = true),
                    @ApiImplicitParam(name = "typeCode", value = "话术分类code,从字典中获取，对应字典的num", dataType = "string", required = true),
                    @ApiImplicitParam(name = "url", value = "地址", dataType = "string", required = false),
                    @ApiImplicitParam(name = "content", value = "内容", dataType = "string", required = false)
            }
    )
    public Object add(@Valid Article article) {
        String TypeCodeName = ConstantFactory.me().getDictNameByNum("verbalType", article.getTypeCode().toString());
        if (ToolUtil.isOneEmpty(article, article.getTitle(), article.getContent(), article.getPic(), TypeCodeName)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        article.setReleaseTime(new Date());
        article.insert();
        return SUCCESS_TIP;
    }

    /**
     * 删除文章
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer articleId) {
        articleService.deleteById(articleId);
        return SUCCESS_TIP;
    }

    /**
     * 修改文章
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @ApiOperation(value = "更新文章", tags = "话术", httpMethod = "POST", code = 200, response = SuccessTip.class)
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "id", dataType = "integer", required = true),
                    @ApiImplicitParam(name = "pic", value = "图片url", dataType = "string", required = true),
                    @ApiImplicitParam(name = "title", value = "标题", dataType = "string", required = true),
                    @ApiImplicitParam(name = "typeCode", value = "话术分类code,从字典中获取，对应字典的num", dataType = "string", required = true),
                    @ApiImplicitParam(name = "url", value = "地址", dataType = "string", required = false),
                    @ApiImplicitParam(name = "content", value = "内容", dataType = "string", required = false)
            }
    )
    public Object update(@Valid Article article) {
        articleService.updateById(article);
        return SUCCESS_TIP;
    }


    /**
     * 文章详情
     */
    @RequestMapping(value = "/detail/{articleId}")
    @ResponseBody
    public Object detail(@PathVariable("articleId") Integer articleId) {
        return articleService.selectById(articleId);
    }
}
